package coms.example.demo.serive.impl;


import coms.example.demo.dao.UserDao;
import coms.example.demo.domain.User;
import coms.example.demo.serive.UserServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserServie  {

     @Autowired
     private UserDao userDao;

     @Autowired
     private RedisTemplate redisTemplate;
     private String USERNAME = "USERNAME:";

    @Override
     public List<User> getUserList() {

        /**
         * 将key为username value为password
         */
        List<User> userList = userDao.getUserList();
        for (User user:userList){
            redisTemplate.opsForValue().set(USERNAME+user.getUsername(),user.getPassword());
            return userDao.getUserList();
        }

        return userList;
     }

    /**
     * 获取用户策略：先从缓存中获取用户，没有则取数据表中
     * 数据，再将数据写入缓存
     */

    @Override
    public User getUserByid (Integer id) {
        String key = "user_"+id;
        ValueOperations<String,User> operations = redisTemplate.opsForValue();

        boolean  hasKey = redisTemplate.hasKey(key);
        //缓存存在
        if(hasKey){
            User user = operations.get(key);
            System.out.println("==========从缓存中获得数据=========");
            System.out.println(user.getUsername());
            System.out.println("==============================");
            return user;
        }else{
            User user = userDao.getUserByid(id);
            System.out.println("==========从数据表中获得数据=========");
            System.out.println(user.getUsername());
            System.out.println("==============================");
            //插入缓存
            operations.set(key, user,5, TimeUnit.MINUTES);
            return user;
        }

    }

    /**
     * 更新用户策略：先更新数据表，成功之后，再更新缓存
     */
    @Override
    public int edit(User user) {
         int result=userDao.edit(user);

        //更新成功
        if(result>0){
            //缓存存在？
            String key = "user_"+user.getId();
            boolean hasKey = redisTemplate.hasKey(key);
            if(hasKey){
                redisTemplate.delete(key);
                System.out.println("删除缓存中的key=========>"+key);
            }
        }
        return result;
    }

    /**
     * 删除用户策略：删除数据表中数据，然后删除缓存
     */
    @Override
    public int delete(Integer id) {
        int result = userDao.delete(id);
        //删除成功
        if(result>0){
            String key = "user_"+id;
            boolean hasKey = redisTemplate.hasKey(key);
            if(hasKey){
                redisTemplate.delete(key);
                System.out.println("删除缓存中的key=========>"+key);
            }
        }
        return result;
    }
    @Override
    public void add(User user) {
        user.setBirthday(new Date());
        userDao.save(user);
    }

}





